package com.test.Protection;

import cn.hutool.core.util.ObjectUtil;
import com.test.license.License;
import com.test.security.a.SignProvider;


import java.util.Properties;
import java.util.TreeMap;


/**
 * @author: zouren
 * @date: 2022/3/14
 * @description:
 */
public class C {
    public static final String KEY_SIGNATURE = "signature";
    public static final String KEY_SP = "sp";

    /**
     * 校验 signature
     * @param properties
     * @return
     * @throws Exception
     */
    public static boolean a(Properties properties){
        Properties emp =  ObjectUtil.clone(properties);
        byte[] s = properties.getProperty(KEY_SIGNATURE).getBytes();
        byte[] sp = properties.getProperty(KEY_SP).getBytes();
        emp.remove(KEY_SIGNATURE);
        emp.remove(KEY_SP);
        TreeMap t = new TreeMap<>();
        t.putAll(emp);
        return SignProvider.verify(sp,t.toString(),s);
    }
    public static boolean a(License license, String sp){
        TreeMap t = new TreeMap<>();
        t.put(License.KEY_APPID,license.getAppId());
        t.put(License.KEY_EXPIRESDATE,license.getExpiresDateStr());
        t.put(License.KEY_INSTALLDATE,license.getInstallDateStr());
        t.put(License.KEY_LICENSETYPE,license.getLicenseType());
        t.put(License.KEY_PRODUCT,license.getProduct());
        t.put(License.KEY_TRYDAYS,license.getTryDays()+"");
        t.put(License.KEY_VERSION,license.getVersion());
        return SignProvider.verify(sp.getBytes(),t.toString(),license.getSignature().getBytes());
    }

}
